package com.shruijin.wy.thread.aqs;

import lombok.extern.slf4j.Slf4j;

import java.util.Collection;
import java.util.List;
import java.util.concurrent.*;

@Slf4j
public class CountDownLatchExample {

    private final static int totalCount=1000;


    public static void main(String[] args) throws InterruptedException {
        ExecutorService exec = Executors.newCachedThreadPool();
        final CountDownLatch countDownLatch = new CountDownLatch(totalCount);

        for (int i=0;i<totalCount;i++){
            final  int threadNum  = i;
            exec.execute(()->{
                try{
                    test(threadNum);
                } catch (Exception e){
                    log.error("exception {}",e);
                } finally {
                    countDownLatch.countDown();
                }
            });

        }
        countDownLatch.await();
        log.info("finished!");
        exec.shutdown();
    }

    private  static void test(int threadNum) throws Exception{
        Thread.sleep(100);
        log.info("thread number is :{}",threadNum);
        Thread.sleep(100);
    }
}
